package io.github.wppli.infrastructure.persistent.repository;

import io.github.wppli.domain.pay.model.valobj.ProductStatus;
import io.github.wppli.domain.recommond.model.entity.ProductEntity;
import io.github.wppli.domain.recommond.repository.IProductRepository;
import io.github.wppli.infrastructure.persistent.dao.IProductDao;
import io.github.wppli.infrastructure.persistent.po.Product;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author li--jiaqiang 2025−03−12
 */
@Repository
@AllArgsConstructor
public class ProductRepository implements IProductRepository {

    private final IProductDao productDao;

    @Override
    public ProductEntity queryById(Long productId) {
        Product product = productDao.selectById(productId);
        return ProductEntity.builder()
                .id(product.getId())
                .name(product.getName())
                .mainImage(product.getMainImage())
                .detailImages(product.getDetailImages())
                .price(product.getPrice())
                .marketPrice(product.getMarketPrice())
                .stock(product.getStock())
                .categoryId(product.getCategoryId())
                .brandId(product.getBrandId())
                .specs(product.getSpecs())
                .description(product.getDescription())
                .build();
    }

    @Override
    public List<ProductEntity> getTopHotProducts(int topN) {
        List<Product> hotProducts = productDao.getTopHotProducts(topN);
        if (hotProducts == null || hotProducts.isEmpty()) {
            return Collections.emptyList();
        }
        return getProductEntities(hotProducts);
    }

    @Override
    public List<ProductEntity> queryByIds(List<Long> products) {
        List<Product> productList = productDao.selectBatchIds(products);
        if (productList.isEmpty()) {
            return Collections.emptyList();
        }
        return getProductEntities(productList);
    }

    @Override
    public io.github.wppli.domain.pay.model.entity.ProductEntity queryProduct(Long productId) {
        Product product = productDao.selectById(productId);
        if (product == null) return null;
        io.github.wppli.domain.pay.model.entity.ProductEntity productEntity = new io.github.wppli.domain.pay.model.entity.ProductEntity();
        productEntity.setProductId(product.getId());
        productEntity.setProductName(product.getName());
        productEntity.setProductDesc(product.getDescription());
        productEntity.setPrice(product.getPrice());
        productEntity.setMainImage(product.getMainImage());
        productEntity.setSpecs(product.getSpecs());
        productEntity.setStock(product.getStock());
        productEntity.setEnable(ProductStatus.get(product.getStatus()));
        return productEntity;
    }

    @NotNull
    private List<ProductEntity> getProductEntities(List<Product> productList) {
        List<ProductEntity> productEntityList = new LinkedList<>();
        for (Product product : productList) {
            ProductEntity productEntity = ProductEntity.builder()
                    .id(product.getId())
                    .name(product.getName())
                    .mainImage(product.getMainImage())
                    .detailImages(product.getDetailImages())
                    .price(product.getPrice())
                    .marketPrice(product.getMarketPrice())
                    .stock(product.getStock())
                    .categoryId(product.getCategoryId())
                    .brandId(product.getBrandId())
                    .specs(product.getSpecs())
                    .description(product.getDescription())
                    .build();
            productEntityList.add(productEntity);
        }
        return productEntityList;
    }
}